﻿/*
* This software is the confidential and proprietary
* information of NetBay
* Such confidential information should not be disclosed
* NetBay copyright(c)
*/
#region Using
using System;
using System.Collections.Generic;
using System.Data;
using NetBay.Core.BusinessEntities.Workflow;
using NetBay.Core.Log;
using NetBay.Core.Extensions;
using NetBay.Core.BusinessEntities;

#endregion

namespace Rio.Flow.Business.Repository.Translator
{
    public partial class WorkflowInstanceTranslator
    {
        #region Public functions
        /// <summary>
        /// Translate a list of WorkflowInstance
        /// </summary>
        /// <param name="DbDataReader"></param>
        /// <returns></returns>
        public static List<WorkflowInstance> TranslateWorkflowInstances(IDataReader DbDataReader)
        {
            var instances = new List<WorkflowInstance>();

            try
            {
                using (DbDataReader)
                {
                    // Read all rows
                    while (DbDataReader.Read())
                    {
                        WorkflowInstance instance = TranslateOneInstance(DbDataReader);
                        instances.Add(instance);
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return instances;
        }

        /// <summary>
        /// Translate one WorkflowInstance
        /// </summary>
        /// <param name="DbDataReader"></param>
        /// <returns></returns>
        public static WorkflowInstance TranslateWorkflowInstance(IDataReader DbDataReader)
        {
            var instance = new WorkflowInstance();

            try
            {
                using (DbDataReader)
                {
                    if (DbDataReader.Read())
                        instance = TranslateOneInstance(DbDataReader);
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return instance;
        }

        /// <summary>
        /// Translate a list of DocumentState
        /// </summary>
        /// <param name="DbDataReader"></param>
        /// <returns></returns>
        public static List<WorkflowItem> TranslateDocumentState(IDataReader DbDataReader)
        {
            var instances = new List<WorkflowItem>();

            try
            {
                using (DbDataReader)
                {
                    // Read all rows
                    while (DbDataReader.Read())
                    {
                        WorkflowDocument model = TranslateOneDocState(DbDataReader);
                        instances.Add(model);
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return instances;
        }

        /// <summary>
        /// Translate a list of DocumentState
        /// </summary>
        /// <param name="DbDataReader"></param>
        /// <returns></returns>
        public static List<WorkflowDocument> TranslateWorkflowDocumentState(IDataReader DbDataReader)
        {
            var instances = new List<WorkflowDocument>();

            try
            {
                using (DbDataReader)
                {
                    // Read all rows
                    while (DbDataReader.Read())
                    {
                        WorkflowDocument model = TranslateOneDocState(DbDataReader);
                        instances.Add(model);
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return instances;
        }

        /// <summary>
        /// Translate a list of InstanceUser
        /// </summary>
        /// <param name="DbDataReader"></param>
        /// <returns></returns>
        public static List<WorkflowInstanceUser> TranslateUserInstances(IDataReader DbDataReader)
        {
            var instances = new List<WorkflowInstanceUser>();

            try
            {
                using (DbDataReader)
                {
                    // Read all rows
                    while (DbDataReader.Read())
                    {
                        instances.Add(TranslateOneInstanceUser(DbDataReader));
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return instances;
        }


        /// <summary>
        /// Translate a list of WkfTuple
        /// </summary>
        /// <param name="DbDataReader"></param>
        /// <returns></returns>
        public static List<WkfTuple> TranslateTuple(IDataReader DbDataReader)
        {
            var result = new List<WkfTuple>();

            try
            {
                using (DbDataReader)
                {
                    // Read all rows
                    while (DbDataReader.Read())
                    {
                        var tutu = new WkfTuple();
                        tutu.OrganizationId = DbDataReader.GetLongValue("RP_TD_RE_ID");
                        tutu.FolderTypeId = DbDataReader.GetLongValue("rp_td_id");
                        tutu.MessageCount = DbDataReader.GetLongValue("nb_instance");
                        tutu.MessageTotalCount = DbDataReader.GetLongValue("nb_instance_totale");
                        result.Add(tutu);
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return result;
        }

        /// <summary>
        /// Translates a list of workflow histo.
        /// </summary>
        /// <param name="DbDataReader">The db data reader.</param>
        /// <returns></returns>
        public static List<WorkflowHisto> TranslateHisto(IDataReader DbDataReader)
        {
            var result = new List<WorkflowHisto>();

            try
            {
                using (DbDataReader)
                {
                    // Read all rows
                    while (DbDataReader.Read())
                    {
                        var hihi = new WorkflowHisto();
                        var l_oEmetteur = new User();

                        //Sender
                        l_oEmetteur.UserId = DbDataReader.GetLongValue("ruser_no");
                        l_oEmetteur.Email = DbDataReader.GetStringValue("ruser_email");
                        l_oEmetteur.Login = DbDataReader.GetStringValue("ruser_login");
                        l_oEmetteur.LastName = DbDataReader.GetStringValue("ruser_nom");
                        l_oEmetteur.FirstName = DbDataReader.GetStringValue("ruser_prenom");
                        hihi.HistoUser = l_oEmetteur;
                        hihi.HistoUserFullName = l_oEmetteur.FullName;

                        hihi.State = DbDataReader.GetEnumValue<WorkflowState>("t_ref_wkf_state_id");
                        hihi.ActionLabel = DbDataReader.GetStringValue("t_ref_wkf_state_desc");
                        hihi.DateAction = DbDataReader.GetDateTimeValue("r_wkf_instance_hist_creation"); 
                        hihi.ActionOrConditionId = DbDataReader.GetLongValue("t_ref_wkf_act_cond_id");
                        hihi.Identifier = DbDataReader.GetLongValue("t_ref_wkf_act_cond_id");
                        hihi.DocumentId = DbDataReader.GetLongValue("rd_no");
                        hihi.InstanceIdentifier = DbDataReader.GetLongValue("r_wkf_instance_id");
                        hihi.DocumentName = DbDataReader.GetStringValue("rd_fichier_origine");
                        hihi.Comment = DbDataReader.GetStringValue("r_wkf_doc_state_comment");
                        hihi.HistoGroupName = DbDataReader.GetStringValue("rg_nom");

                        result.Add(hihi);
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return result;
        }

        #endregion

        #region Private functions

        /// <summary>
        /// Translates one WorkflowInstance
        /// </summary>
        /// <param name="DbDataReader">The db data reader.</param>
        /// <returns></returns>
        private static WorkflowInstance TranslateOneInstance(IDataReader DbDataReader)
        {
            var instance = new WorkflowInstance();
            var l_oEmetteur = new User();

            try
            {
                //Sender
                l_oEmetteur.UserId = DbDataReader.GetLongValue("ruser_no");
                l_oEmetteur.Email = DbDataReader.GetStringValue("ruser_email");
                l_oEmetteur.Login = DbDataReader.GetStringValue("ruser_login");
                l_oEmetteur.LastName = DbDataReader.GetStringValue("ruser_nom");
                l_oEmetteur.FirstName = DbDataReader.GetStringValue("ruser_prenom");
                instance.Sender = l_oEmetteur;
                instance.SendedBy = DbDataReader.GetStringValue("Sender");

                //instance
                instance.Identifier = DbDataReader.GetLongValue("r_wkf_instance_id");
                instance.DefinitionId = DbDataReader.GetLongValue("r_wkf_def_id");
                instance.DefinitionName = DbDataReader.GetStringValue("r_wkf_def_name"); 
                instance.FolderId = DbDataReader.GetLongValue("td_no");
                instance.State = DbDataReader.GetEnumValue<WorkflowState>("t_ref_wkf_state_id");
                instance.CreatedOn = DbDataReader.GetDateTimeValue("r_date_creation"); 

                if (DbDataReader["r_wkf_instance_id_parent"] != DBNull.Value && !string.IsNullOrEmpty(DbDataReader["r_wkf_instance_id_parent"].ToString()))
                {
                    instance.ParentId = DbDataReader.GetLongValue("r_wkf_instance_id_parent");
                }

                if (DbDataReader["rg_id"] != DBNull.Value)
                {
                    instance.GroupId = DbDataReader.GetLongValue("rg_id");
                    instance.RguId = DbDataReader.GetLongValue("rgu_id");
                }

                instance.ChronoId = DbDataReader.GetLongValue("chrono_id");
                instance.Receivers = DbDataReader.GetStringValue("r_wkf_instance_receivers");
                instance.IsEnded = DbDataReader.GetBooleanValue("r_wkf_instance_ended");
                instance.StartingCondition = DbDataReader.GetLongValue("r_wkf_def_act_cond_id");
                instance.StartingConditionLabel = DbDataReader.GetStringValue("t_ref_wkf_act_cond_name");  
                instance.LastReadDate = DbDataReader.GetDateTimeValue("r_wkf_instance_read_date"); 
                instance.LastActionDate = DbDataReader.GetDateTimeValue("r_wkf_instance_action_date"); 
                instance.IsRead = DbDataReader.GetBooleanValue("IS_READ");
                instance.InstanceUserId = DbDataReader.GetLongValue("r_wkf_instance_usr_id");
                if (DbDataReader["rg_nom"] != DBNull.Value)
                {
                    instance.GroupName = DbDataReader.GetStringValue("rg_nom");
                }

                instance.StateLabel = DbDataReader.GetStringValue("t_ref_wkf_state_name");
                instance.UniqueGroupInstanceId = DbDataReader.GetLongValue("r_wkf_instance_group_id");

                instance.HasChild = DbDataReader.GetBooleanValue("HasChild"); ;

            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return instance;
        }

        /// <summary>
        /// Translates one WorkflowInstance
        /// </summary>
        /// <param name="DbDataReader">The db data reader.</param>
        /// <returns></returns>
        private static WorkflowDocument TranslateOneDocState(IDataReader DbDataReader)
        {
            var document = new WorkflowDocument();
            try
            {
                document.Identifier = DbDataReader.GetLongValue("r_wkf_doc_state_id");
                document.DocumentId = DbDataReader.GetLongValue("rd_no");
                document.Name = DbDataReader.GetStringValue("rd_fichier_origine");
                document.InstanceId = DbDataReader.GetLongValue("r_wkf_instance_id");
                document.Comment = DbDataReader.GetStringValue("r_wkf_doc_state_comment");
                document.CreatedOn = DbDataReader.GetDateTimeValue("r_wkf_doc_state_creation");
                document.ModifiedOn = DbDataReader["r_wkf_doc_state_modified"] != DBNull.Value ? DbDataReader.GetDateTimeValue("r_wkf_doc_state_modified") : new Nullable<DateTime>();
                document.AchievedBy = DbDataReader.GetStringValue("userFullName");
                document.DocumentStatus = DbDataReader.GetEnumValue<WorkflowState>("t_ref_wkf_state_id");
                document.GroupUserId = DbDataReader.GetLongValue("rgu_id"); 
                document.DocumentStatusLabel = DbDataReader.GetStringValue("t_ref_wkf_state_desc");
                document.LastActionId = DbDataReader.GetLongValue("t_ref_wkf_act_cond_id"); 
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }
            return document;
        }

        /// <summary>
        /// Translates one InstanceUser
        /// </summary>
        /// <param name="DbDataReader">The db data reader.</param>
        /// <returns></returns>
        private static WorkflowInstanceUser TranslateOneInstanceUser(IDataReader DbDataReader)
        {
            var instance = new WorkflowInstanceUser();

            try
            {
                instance.Identifier = DbDataReader.GetLongValue("r_wkf_instance_usr_id");
                instance.InstanceId = DbDataReader.GetLongValue("r_wkf_instance_id");
                instance.UserGroupId = DbDataReader.GetLongValue("rgu_id");
                instance.IsDeleted = DbDataReader.GetBooleanValue("r_wkf_instance_usr_deleted");
                instance.LastActionId = DbDataReader.GetLongValue("r_wkf_instance_usr_lastAction");
                instance.IsEnded = DbDataReader.GetBooleanValue("r_wkf_instance_usr_ended");
                instance.IsRead = DbDataReader.GetBooleanValue("r_wkf_instance_usr_readed");
                instance.GroupName = DbDataReader.GetStringValue("rg_nom");
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return instance;
        }

        #endregion
    }
}